  /**
 *ROOM HOME SHUOSHUO
 * author:liyi
 */
var SHUOSHUO_ROWS = 10;

/**
 * 
 */
function isRoomMaster(){
	var v = $("#topic_roommaster").val();
	if(v==1){
		return true;//\u662f\u623f\u4e3b
	}else{
		return false;
	}
}
/**
 * textObj:
 * surplusObj
 * count
 */
function wordNumberChange(textObj,surplusObj,count){
	var text = textObj.value;
	var surplus = count - text.len();
	if(surplus<0){
		textObj.value="";
		var j=0;
		for(var i=0;j<=count&&i<text.length;i++){
			textObj.value+=text.charAt(i);
			j=textObj.value.len();
			
		}
		if(j>count){
			textObj.value=textObj.value.substring(0,textObj.value.length-1);
		}
		surplusObj.text(0);
		surplusObj.innerHTML=0;
	}else{
		surplusObj.text(Math.floor(surplus));
		surplusObj.innerHTML=Math.floor(surplus);
	}
	
}
/**
 * 
 * @param textObj
 * @param eventType  onblur --失去焦点 onfocus--获得焦点
 * @returns
 */
function shuoshuoWordChange(textObj,eventType){
	
	if(eventType == "onfocus"){
		if(textObj.value==$(textObj).attr("default-text")){
			textObj.value="";
			$(textObj).removeClass("textSty");
		}else if(textObj.value.indexOf($(textObj).attr("default-text"))>-1){
			
			textObj.value=textObj.value.substring($(textObj).attr("default-text").length);
			$(textObj).removeClass("textSty");
		}
	}else if(eventType == "onblur"){
		if(textObj.value==""){
			textObj.value = $(textObj).attr("default-text");
			$(textObj).addClass("textSty");
			return;
		}
	}
	
	
	var surplusObj =$(".surplusNum");//
	
	
	wordNumberChange(textObj,surplusObj,140);
}
/**
 * \u53d1\u5e03\u8bf4\u8bf4
 */
function publishShuoshuo(url){
	
	
	/*************非登陆用户***********/
	if($("#topic_userid").val()==-1){
		alert("\u8bf7\u5148\u767b\u5f55!");
		return;
	}
	
	
	var surplusNum = $(".surplusNum").text();
	if(surplusNum<0){
		alert("\u53d1\u5e03\u5b57\u6570\u8d85\u8fc7\u9650\u5236!");
		return;
	}
	var isSyn2MySpaceChk = $("#syn2MySpaceChk").attr("checked");//
	var content = forceWriteHTML($("#publishShuoshuoText").val());
	var roomId = $("#topic_roomid").val();
	var jsonData = {roomId:roomId,content:content,isSyn2MySpaceChk:isSyn2MySpaceChk};
	if(content.Trim()=="" || content==$("#publishShuoshuoText").attr("default-text")){
		alertDiv('',"\u5199\u70b9\u4e1c\u897f\u5427\uff0c\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\u54e6\u3002",-1);
//		$("#publishShuoshuoText").focus();
		return ;
	}
	
	$.ajax({
		   type: "POST",
		   url: url,
		   data: jsonData,
		   dataType:"json",
		   success: function(msg){
		     var result = msg.result;
		     if(result){
		    	 alertImg("alert_fbcg",3000);
		    	 $("#publishShuoshuoText").val("");
		    	 shuoshuoWordChange(document.getElementById("publishShuoshuoText"));
		    	 findAllShuoShuo(1);
		     }else{
		    	 alertImg("alert_czsb",3000);
		     }
		     return ;
		   }
	});
}
/**
 * \u52a0\u8f7d\u8bf4\u8bf4
 */
function findAllShuoShuo(pageNo){
	var url="/topic!findTopicAll.jspa";
	var roomId = $("#topic_roomid").val();
	var shuoshuoNbList =$(".shuoshuoNbList");
	if(pageNo ==undefined || pageNo<=0){
		pageNo = 1;
	}
	var jsonData={pageNo:pageNo,roomId:roomId};
	$.ajax({
		   type: "POST",
		   url: url,
		   data: jsonData,
		   dataType:"json",
		   success: function(msg){
		     var count = msg.count;
		     var pageNo = msg.pageNo;
		     var listData = msg.listdata;
		     
		     showSHUOSHUOList(listData);
		     var pageHTML = pagelistHTML(count,pageNo,"findAllShuoShuo(");
		     shuoshuoNbList.html(pageHTML);
		     return ;
		   }
	});
}
/**
 * \u5c06list\u6570\u636e\u663e\u793a\u5728\u9875\u9762\u4e0a
 * @param list 
 */
function showSHUOSHUOList(list){
	$(".shuoshuolTab2").html("");
	$(list).each(function(i,item){
		var content = RepHtml(item.content);
		var html="<li><a  target=\"_blank\" href=\"http://zone.17guagua.com/"+item.user_id+"\" class=\"leftAvatar\"><img src=\"http://zone1.ggimg.cn/custom/portrait/"+item.user_id+"_small.jpg\" name=\"infocard"+item.user_id+"\"></a><div class=\"rightItem\"><p class=\"rI_info\"><a target=\"_blank\" href=\"http://zone.17guagua.com/"+item.user_id+"\" >"+item.user_nick+"</a>\uff1a"+content+"</p><p class=\"rI_detail\"><span>"+item.date+"</span><span>"+item.time+"</span><a href=\"javascript:void(0)\" class=\"commentCount_"+item.id+"\" onclick=\"showcomment('"+item.id+"','"+item.user_id+"');return false;\">\u8bc4\u8bba";
		if(item.comment_count>0){
			html+="("+item.comment_count+")";
		}
		html+="</a>";
//	    alert(item.user_id +"_"+$("#topic_userid").val());
		if(isRoomMaster() || item.user_id == $("#topic_userid").val()){//房主可以删除所有话题，其他用户可以删除自己的话题
	    	html+="<a href=\"javascript:void(0)\" onclick=\"deleteShuoShuo('"+item.id+"');return false;\">\u5220\u9664</a>";
	    }
	    html+="</p>";
		//\u8bc4\u8bba
		html+="<div topicid=\""+item.id+"\" style=\"display:none\" class=\"rI_review  shuoshuoComment\"><div class=\"rI_signed\"><input type=\"text\" maxlength=140 id=\"comment_text_"+item.id+"\"/><a class=\"ri_but1\" href=\"javascript:void(0)\" onclick=\"publishComment('"+item.id+"');return false;\"></a></div><div class=\"wInfo2\"><a href=\"javascript:void(0)\" class=\"phizOne commentface\" dbid=\""+item.id+"\" isload=\"false\"><i class=\"iconA\"></i>\u8868\u60c5</a></div>";
		html+="<dl class=\"rI_reviewsList2 commentlist_"+item.id+"\"></dl>";
		//\u5206\u9875
		html+="<ul class=\"nbList commentNbList_"+item.id+"\"></ul>";
		html+="</div></div></li>";
		$(".shuoshuolTab2").html($(".shuoshuolTab2").html()+html);
	});
	if($(".shuoshuolTab2").html()==""){
		$(".shuoshuolTab2").html("<img src=\"/images/sf_bg.jpg\"/>");
	}
}

/**
 * \u8fd4\u56de\u5206\u9875\u5217\u8868HTML
 * @param count \u6570\u636e\u603b\u6570
 * @param pageNo  \u5f53\u524d\u9875
 * @param eventName  \u5206\u9875\u4e8b\u4ef6\u540d\u79f0
 */
function pagelistHTML(count,pageNo,eventName){
	var pageCount=Math.ceil(count/SHUOSHUO_ROWS);
	if(pageCount<=1){
		return "";
	}
	var html="";
	if(pageNo>1 && pageCount>0){
		html = "<a href=\"javascript:void(0)\" onclick=\""+eventName+"'"+1+"');return false;\">\u9996\u9875</a>";
		html += "<a href=\"javascript:void(0)\" onclick=\""+eventName+"'"+(pageNo-1)+"');return false;\">\u4e0a\u4e00\u9875</a>";
	}
	
	var start = pageNo-4;
	if(((start+9) - pageCount)>0){
		start = start-((start+9) - pageCount);
		
	}
	if(start<1) start =1;
	for(var j=1,i=start;i<=pageCount && j<=10;j++,i++){
		if(i==pageNo){
			html +="<a href=\"javascript:void(0)\" class=\"on\" onclick=\""+eventName+"'"+i+"');return false;\">"+i+"</a>";
		}else{
			html +="<a href=\"javascript:void(0)\" onclick=\""+eventName+"'"+i+"');return false;\">"+i+"</a>";
		}
	}
	if(pageNo != pageCount && pageCount>0){
		html += "<a href=\"javascript:void(0)\" onclick=\""+eventName+"'"+(pageNo+1)+"');return false;\">\u4e0b\u4e00\u9875</a>";
		html += "<a href=\"javascript:void(0)\" onclick=\""+eventName+"'"+(pageCount)+"');return false;\">\u672b\u9875</a>";
	}
	
	return html;
}

/**
 * \u5220\u9664\u8bf4\u8bf4\u4e3b\u9898
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * \u6210\u529f\u540e\u8fd4\u56de\u7b2c\u4e00\u9875
 * 
 */
function deleteShuoShuo(shuoshuoId){
	if(!confirm("\u786e\u8ba4\u5220\u9664\uff1f")){
		return false;
	}
	var url ="/topic!delTopicById.jspa";
	var roomId = $("#topic_roomid").val();
	var jsonData={id:shuoshuoId,roomId:roomId};
		$.ajax({
			   type: "POST",
			   url: url,
			   data: jsonData,
			   dataType:"json",
			   success: function(msg){
			     	var result = msg.result;
				     if(result){//\u5220\u9664\u6210\u529f
				    	 alertImg("alert_sccg",3000);
				    	 findAllShuoShuo(1);//\u66f4\u65b0\u7b2c\u4e00\u9875
				     }else{//\u5220\u9664\u5931\u8d25
				    	 alertImg("alert_czsb",3000);
				     }
			   }
		});

}
/**
 * \u5173\u95ed\u5176\u4ed6\u5df2\u663e\u793a\u7684\u8bc4\u8bba,\u663e\u793a\u5f53\u524d\u4e3b\u9898\u8bc4\u8bba
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * \u6210\u529f\u540e\u8fd4\u56de\u7b2c\u4e00\u9875
 * @param publishUserId 发布者ID
 */
function showcomment(shuoshuoId,publishUserId){
	$(".shuoshuoComment").each(function(index,item){
		if($(item).attr("topicid")!=shuoshuoId){//\u4e0d\u662f\u5f53\u524d
			$(item).hide();
		}else{
			 if($(item).is(":hidden")){//\u662f\u5426\u9690\u85cf 
			 	//$(item).show();
			 	//\u53bb\u52a0\u8f7d\u8bc4\u8bba
			 	loadcomments(shuoshuoId,publishUserId,1,$(item));
			 }else{
			    $(item).hide();
			 }
           
			
		}
	});
}

function loadcomments(shuoshuoId,publishUserId,pageNo,itemDiv){
   if(itemDiv == undefined){
   	$(".shuoshuoComment").each(function(index,item){
		if($(item).attr("topicid")==shuoshuoId){//\u4e0d\u662f\u5f53\u524d
		 itemDiv = $(item);
		}
	});
   }
   var url ="/topic!findCommentByTopicId.jspa";
   var roomId = $("#topic_roomid").val();
   var jsonData={id:shuoshuoId,roomId:roomId,pageNo:pageNo};
	$.ajax({
			   type: "POST",
			   url: url,
			   data: jsonData,
			   dataType:"json",
			   success: function(msg){
			    var count = msg.count;//\u4e2a\u6570
		        var pageNo = msg.pageNo;//\u5f53\u524d\u9875\u6570
		        var listData = msg.listdata;
		        //\u4fee\u6539\u4e2a\u6570\u663e\u793a 
		        if(count>0){
		        	 $(".commentCount_"+shuoshuoId).html("\u8bc4\u8bba("+count+")");
		        }else{
		        	$(".commentCount_"+shuoshuoId).html("\u8bc4\u8bba");
		        }
		       
		        //\u5206\u9875 commentNbList_
		       showSHUOSHUOCommentList(listData,shuoshuoId,publishUserId);
		        //\u663e\u793a\u5206\u9875\u5217\u8868
		     var pageHTML = pagelistHTML(count,pageNo,"loadcomments("+shuoshuoId+","+publishUserId+",");
		     $(".commentNbList_"+shuoshuoId).html(pageHTML);
		     
		     loadcommentfaceEvent();
		        itemDiv.show();
		     return ;
			     	
			   }
		});

}
/**
 * \u52a0\u8f7d\u8bc4\u8bba
 * @param list \u6570\u636e
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * \u6210\u529f\u540e\u8fd4\u56de\u7b2c\u4e00\u9875
 * 
 */
function showSHUOSHUOCommentList(list,shuoshuoId,publishUserId){
    $(".commentlist_"+shuoshuoId).html("");
	$(list).each(function(index,item){
		var content = RepHtml(item.content);
		var html="<dd><a target=\"_blank\" href=\"http://zone.17guagua.com/"+item.user_id+"\" class=\"leftAvatar2\"><img src=\"http://zone1.ggimg.cn/custom/portrait/"+item.user_id+"_small.jpg\" name=\"infocard"+item.user_id+"\" width=\"32\" height=\"32\"></a><div class=\"rightItem2\"><p class=\"rI_info2\"><a target=\"_blank\" href=\"http://zone.17guagua.com/"+item.user_id+"\">"+item.user_nick+"</a>\uff1a"+content+"</p><p class=\"rI_detail\"><span>"+item.date+"</span><span>"+item.time+"</span>";
		if(isRoomMaster() || item.user_id == $("#topic_userid").val()){
			html+="<a href=\"javascript:void(0)\" onclick=\"deleteComment('"+item.parent_id+"','"+item.id+"');return false;\">\u5220\u9664</a>";
		}
		//话题的发起者可以回复，其他人不可以回复
		if(publishUserId == $("#topic_userid").val()){
			html+="<a href=\"javascript:void(0)\" onclick=\"revertComment('"+item.parent_id+"','"+item.id+"','"+item.user_nick+"');return false;\">\u56de\u590d</a>";
		}
		html+="</p></div></dd>";
	    $(".commentlist_"+shuoshuoId).html($(".commentlist_"+shuoshuoId).html()+html);
	});
} 

/**
 * \u53d1\u5e03\u8bf4\u8bf4\u8bc4\u8bba
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * \u6210\u529f\u540e\u8fd4\u56de\u7b2c\u4e00\u9875
 * 
 */
function publishComment(shuoshuoId){
	/*************非登陆用户***********/
	if($("#topic_userid").val()==-1){
		alert("\u8bf7\u5148\u767b\u5f55!");
		return;
	}
	
   var url ="/topic!createComment.jspa";
	var content = $("#comment_text_"+shuoshuoId).val();//\u5185\u5bb9
	if(content.Trim()==""){
		alertDiv('',"\u5199\u70b9\u4e1c\u897f\u5427\uff0c\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\u54e6\u3002",-1);
//		$("#comment_text_"+shuoshuoId).focus();
		return ;
	}
	content = forceWriteHTML(content);
	var roomId = $("#topic_roomid").val();
	var parentId = shuoshuoId;
	var jsonData = {roomId:roomId,content:content,parentId:parentId};
	$.ajax({
		   type: "POST",
		   url: url,
		   data: jsonData,
		   dataType:"json",
		   success: function(msg){
		     var result = msg.result;
		     if(result){//\u53d1\u5e03\u6210\u529f
		    	 alertImg("alert_plcg",3000);
		    	 $("#comment_text_"+shuoshuoId).val("");
		    	loadcomments(shuoshuoId,1);//\u91cd\u65b0\u52a0\u8f7d 
		     }else{//\u53d1\u5e03\u5931\u8d25
		    	 alertImg("alert_czsb",3000);
		     }
		     return ;
		   }
	});
}
/**
 * \u56de\u590d\u8bc4\u8bba
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * @param commentId  \u8bc4\u8bbaID(\u76ee\u524d\u6ca1\u7528\u4fdd\u7559)
 * @param userNick  \u7528\u6237\u6635\u79f0(\u56de\u590d\u5bf9\u8c61)
 */
function revertComment(shuoshuoId,commentId,userNick){
 	$("#comment_text_"+shuoshuoId).val("\u56de\u590d("+userNick+"):");
}
/**
 * \u5220\u9664\u8bc4\u8bba
 * @param shuoshuoId \u8bf4\u8bf4\u4e3b\u9898ID
 * @param commentId  \u8bc4\u8bbaID(\u76ee\u524d\u6ca1\u7528\u4fdd\u7559)
 */
function deleteComment(shuoshuoId,commentId){
	if(!confirm("\u786e\u8ba4\u5220\u9664\uff1f")){
		return false;
	}
	var url ="/topic!delCommentById.jspa";
	var roomId = $("#topic_roomid").val();
	var jsonData={id:commentId,roomId:roomId,parentId:shuoshuoId};
	$.ajax({
		   type: "POST",
		   url: url,
		   data: jsonData,
		   dataType:"json",
		   success: function(msg){
		     	var result = msg.result;
			     if(result){//\u5220\u9664\u6210\u529f
			    	 alertImg("alert_sccg",3000);
			    	 loadcomments(shuoshuoId,1);//\u66f4\u65b0\u7b2c\u4e00\u9875
			     }else{//\u5220\u9664\u5931\u8d25
			    	 alertImg("alert_czsb",3000);
			     }
		   }
	});
}
function loadcommentfaceEvent(){
	$(".commentface").each(function(index,item){
		var id = $(item).attr("dbid");
		if(id == undefined){
			return ;
		}
		var isload = $(item).attr("isload");
		if(isload == "false"){
			$(item).jqfaceedit("comment_text_"+id); //添加表情
			 $(item).attr("isload","true");
		}
		
	});
}
